/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin		 	*/
/*	Project:	GlbRtbEng-1.0.0			*/
/*	Author:		gong_libin			*/
/*	Date:		2016_06_26			*/
/*	File:		GlbRtbIdxs.cpp			*/
/********************************************************/

#include "GlbRtbIdxs.h"

GLBRTBINDEX_C g_CBrw;
GLBRTBINDEX_C g_COsn;
GLBRTBINDEX_C g_CLoc;
GLBRTBINDEX_C g_CMdi;
GLBRTBINDEX_C g_CSrc;
GLBRTBINDEX_C g_CTyp;

void GlbRtbIdxsVal(GLBRAINFO_S** ppstVal, GLBRAINFO_S* pstInfo)
{
	UINT uiCount = 0;

	while (NULL != *(ppstVal + uiCount)) uiCount ++;
	*(ppstVal + uiCount) = pstInfo;

	return;
}

void GlbRtbIdxsPut(GLBRTBINDEX_C& rCMap, char* pszKey, GLBRAINFO_S* pstVal)
{
	GLBSTRING_C CKey;
	GLBRAINFO_S** ppstVal = NULL;

	CKey.append(pszKey);
	if (true != rCMap.GlbMapSelect(CKey, ppstVal, sizeof(GLBRAINFO_S**))) {
		ppstVal = (GLBRAINFO_S**)GlbMalloc(sizeof(GLBRAINFO_S**) * GLB_RA_SUM);
	}
	if (NULL != ppstVal) {
		GlbRtbIdxsVal(ppstVal, pstVal);
		rCMap.GlbMapUpdate(CKey, ppstVal);
	}

	return;
}

void GlbRtbIdxsNew(GlbCls::CGlbSet& rCSet, UCHAR ucType)
{
	UINT uiInfo = 0;
	char* pszRet = NULL;
	char* pszCur = NULL;
	char szBuf[GLB_KBYTES8] = {0};
	GLBRTBINDEX_C* pCIdxs = NULL;

	while (uiInfo < rCSet.GlbSetGetCount()) {
		switch (ucType) {
			case GLB_RI_BROW:
				memcpy(szBuf, ((GLBRAINFO_S*)rCSet.GlbSetIndexGet(uiInfo))->m_szBrow, GLB_KBYTES8);
				pCIdxs = &g_CBrw;
				break;
			case GLB_RI_OSNM:
				memcpy(szBuf, ((GLBRAINFO_S*)rCSet.GlbSetIndexGet(uiInfo))->m_szOsNm, GLB_KBYTES8);
				pCIdxs = &g_COsn;
				break;
			case GLB_RI_LOCA:
				memcpy(szBuf, ((GLBRAINFO_S*)rCSet.GlbSetIndexGet(uiInfo))->m_szLoca, GLB_KBYTES8);
				pCIdxs = &g_CLoc;
				break;
			case GLB_RI_MEDI:
				memcpy(szBuf, ((GLBRAINFO_S*)rCSet.GlbSetIndexGet(uiInfo))->m_szMedi, GLB_KBYTES8);
				pCIdxs = &g_CMdi;
				break;
			case GLB_RI_SCRN:
				memcpy(szBuf, ((GLBRAINFO_S*)rCSet.GlbSetIndexGet(uiInfo))->m_szSrcn, GLB_KBYTES8);
				pCIdxs = &g_CSrc;
				break;
			case GLB_RI_TYPE:
				memcpy(szBuf, ((GLBRAINFO_S*)rCSet.GlbSetIndexGet(uiInfo))->m_szType, GLB_KBYTES8);
				pCIdxs = &g_CTyp;
				break;
			default:
				break;
		}
		pszCur = szBuf;
		while (NULL != (pszRet = (char*)strsep((char**)&pszCur, ","))) {
			GlbRtbIdxsPut(*pCIdxs, pszRet, (GLBRAINFO_S*)rCSet.GlbSetIndexGet(uiInfo));
		}
		memset(szBuf, '\0', GLB_KBYTES8);
		uiInfo += 1;
	}

	return;
}

void GlbRtbIdxsAll(GlbCls::CGlbSet& rCSet)
{
	UINT uiCls = 0, uiNew = 0;
	GLBRTBINDEX_C* pCIdxs[] = {&g_CBrw, &g_COsn, &g_CLoc, &g_CMdi, &g_CSrc, &g_CTyp};
	UCHAR ucType[] = {GLB_RI_BROW, GLB_RI_OSNM, GLB_RI_LOCA, GLB_RI_MEDI, GLB_RI_SCRN, GLB_RI_TYPE};

	while (uiCls < sizeof(pCIdxs) / sizeof(GLBRTBINDEX_C*)) pCIdxs[uiCls ++]->GlbMapClear();
	while (uiNew < sizeof(ucType) / sizeof(UCHAR)) GlbRtbIdxsNew(rCSet, ucType[uiNew ++]);

	return;
}

void GlbRtbIdxsShow()
{
	UINT uiCount = 0;
	GLBRTBINDEX_IT CIt;
	GLBRAINFO_S** ppstInfo = NULL;
	GLBRTBINDEX_C* pCIdxs[] = {&g_CBrw, &g_COsn, &g_CLoc, &g_CMdi, &g_CSrc, &g_CTyp};

	while (uiCount < sizeof(pCIdxs) / sizeof(GLBRTBINDEX_C*)) {
		GLB_PRINT("Idx[%d]: {\n", uiCount);
		CIt = pCIdxs[uiCount]->GlbMapBegin();
		while (CIt != pCIdxs[uiCount]->GlbMapEnd()) {
			GLB_PRINT("%s: {", CIt->first.c_str());
			ppstInfo = (GLBRAINFO_S**)CIt->second;
			while (NULL != *ppstInfo) {
				GLB_PRINT("%s, ", (*ppstInfo)->m_szAdId);
				ppstInfo += 1;
			}
			GLB_PRINT("}\n");
			CIt ++;
		}
		GLB_PRINT("}\n\n");
		uiCount += 1;
	}

	return;
}
